package com.cx.base.rx;


import com.cx.base.model.INetResult;
import com.cx.base.model.IPageList;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.ObservableTransformer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Function;
import io.reactivex.functions.Predicate;
import io.reactivex.schedulers.Schedulers;

public class RxHelper {
    /**
     * IO线程和main线程之间的切换
     *
     * @param <T>
     * @return
     */
    public static <T> ObservableTransformer<T, T> io_main() {
        return new ObservableTransformer<T, T>() {
            @Override
            public ObservableSource<T> apply(Observable<T> upstream) {
                return upstream
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread());
            }
        };
    }


    /**
     * 校验返回后端返回的code，并转成结果
     *
     * @param <T> 结果
     * @return 结果
     */
    public static <T> ObservableTransformer<INetResult<T>, T> result_data() {
        return new ObservableTransformer<INetResult<T>, T>() {
            @Override
            public ObservableSource<T> apply(Observable<INetResult<T>> upstream) {
                return upstream
                        .map(new Function<INetResult<T>, T>() {
                            @Override
                            public T apply(INetResult<T> result) throws Exception {
                                result.checkResultCode();
                                result.checkResultData();
                                return result.getResult();
                            }
                        });
            }
        };
    }

    public static <T> ObservableTransformer<IPageList<T>, List<T>> page_list() {
        return new ObservableTransformer<IPageList<T>, List<T>>() {
            @Override
            public ObservableSource<List<T>> apply(Observable<IPageList<T>> upstream) {
                return upstream
                        .map(new Function<IPageList<T>, List<T>>() {
                            @Override
                            public List<T> apply(IPageList<T> tPageList) throws Exception {
                                tPageList.checkResultData();
                                return tPageList.getList();
                            }
                        });
            }
        };
    }

    public static<T extends INetResult>  ObservableTransformer<T, T> result_result() {
        return new ObservableTransformer<T, T>() {
            @Override
            public ObservableSource<T> apply(Observable<T> upstream) {
                return upstream
                        .filter(new Predicate<T>() {
                            @Override
                            public boolean test(T result) throws Exception {
                                result.checkResultCode();
                                return true;
                            }
                        });
            }
        };
    }

    public static <T> ObservableTransformer<INetResult<T>, INetResult<T>> result_result_T() {
        return new ObservableTransformer<INetResult<T>, INetResult<T>>() {
            @Override
            public ObservableSource<INetResult<T>> apply(Observable<INetResult<T>> upstream) {
                return upstream
                        .filter(new Predicate<INetResult<T>>() {
                            @Override
                            public boolean test(INetResult<T> result) throws Exception {
                                result.checkResultCode();
                                result.checkResultData();
                                return true;
                            }
                        });
            }
        };
    }





}
